<?php

namespace App\Service\users;

use App\Models\users\UsersAmountRecordModel;
use App\Models\users\UsersModel;
use lxs\config\BaseStatic;
use lxs\tools\Tools;

class UsersService extends BaseStatic
{

    /**
     * 获取用户信息
     */
    public function getUserInfo($uid, $field = '*'): object|null
    {
        return UsersModel::with(['userAuthentication'])->select($field)->where('id', $uid)->first();
    }


    /**
     * 通过手机号码获取用户信息
     */
    public function getUserInfoByMobile($mobile, $field = '*'): object|null
    {
        return UsersModel::select($field)->where('mobile', $mobile)->first();
    }


    /**
     * 获取用户详细信息
     */
    public function getUserDetail($uid): object|null
    {
        return UsersModel::with(['userBank', 'userAlipay', 'userSetting', 'userAuthentication'])->where('id', $uid)->first();
    }


    /**
     * 获取用户收账户余额数量
     */
    public function getUserBalance($uid): object|null
    {

        $userInfo = $this->getUserInfo($uid);

        $accumulate_credit_inc = UsersAmountRecordModel::where('uid', $uid)
            ->where('deleted_at', 0)
            ->where('credit_type', AccountRecordService::USER_CREDIT)
            ->where('num', '>', 0)
            ->sum('num');
        $accumulate_credit_dec = UsersAmountRecordModel::where('uid', $uid)
            ->where('deleted_at', 0)
            ->where('credit_type', AccountRecordService::USER_CREDIT)
            ->where('num', '<', 0)
            ->sum('num');
        $userInfo->accumulate_credit = $accumulate_credit_inc + $accumulate_credit_dec;

        $userInfo->accumulate_will_withdraw = UsersAmountRecordModel::where('uid', $uid)
            ->where('deleted_at', 0)
            ->where('credit_type', AccountRecordService::USER_AMOUNT)
            ->where('num', '>', 0)
            ->sum('num');
        $userInfo->accumulate_withdraw = UsersAmountRecordModel::where('uid', $uid)
            ->where('deleted_at', 0)
            ->where('type', AccountRecordService::TYPE_WITHDRAW)
            ->where('credit_type', AccountRecordService::USER_AMOUNT)
            ->where('num', '<', 0)
            ->sum('num');
        $userInfo->surplus_will_withdraw = $userInfo->amount;

        $userInfo->accumulate_credit = $userInfo->accumulate_credit == 0 ? '0.00' : '+' . $userInfo->accumulate_credit;
        $userInfo->accumulate_will_withdraw = $userInfo->accumulate_will_withdraw == 0 ? '0.00' : $userInfo->accumulate_will_withdraw;
        $userInfo->accumulate_withdraw = $userInfo->accumulate_withdraw == 0 ? '0.00' : Tools::interceptCustomStr($userInfo->accumulate_withdraw, '-');

        return $userInfo;
    }


    /**
     * 获取用户收益记录明细
     * @param $uid int 用户id
     * @param $page int 页码
     * @param $pageSize int 每页数量
     * @param array|string|null $dateTime array|string 日期
     * @param $type int 收益类型
     * @return array
     */
    public function getUserIncomeRecordData(int $uid, int $page, int $pageSize, array|string|null $dateTime, int $type): array
    {
        $model = UsersAmountRecordModel::where('uid', $uid)
            ->where('deleted_at', 0);

        if ($dateTime !== '') {
            if (is_string($dateTime)) {
                list($start, $end) = Tools::getTheMonth($dateTime);
                $model->whereBetween('create_at', [$start, $end]);
            } elseif (is_array($dateTime)) {
                $model->whereBetween('create_at', $dateTime);
            }
        }

        if ($type) {
            match ($type) {
                1 => $model->where('credit_type', AccountRecordService::USER_CREDIT),
                2 => $model->where('credit_type', AccountRecordService::USER_AMOUNT),
                3 => $model->where('credit_type', AccountRecordService::TYPE_WITHDRAW),
            };
        }

        $count = $model->count();

        $lists = $model->orderBy('create_at', 'desc')
            ->orderBy('id', 'desc')
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->get()->toArray();
        if ($lists) {
            foreach ($lists as $key => $item) {
                $lists[$key]['create_at'] = date('Y-m-d', strtotime($item['create_at']));
                $lists[$key]['type_name'] = UsersAmountRecordModel::getTypeName[$item['type']] ?? '未知';
                $lists[$key]['credit_type_name'] = UsersAmountRecordModel::getCreditTypeName[$item['credit_type']] ?? '未知';
            }
            return array($lists, $count);
        }
        return array([], 0);
    }



}
